<template>
  <div class="user">
    <!-- 未登录的头部 -->
    <div class="My_title" v-if="!isLoading">
      <img src="http://liufusong.top:8080/img/profile/bg.png" alt="">
      <div class="My_info">
        <!-- 头像 -->
        <div class="My_myIcon">
          <img src="http://liufusong.top:8080/img/profile/avatar.png" alt="">
        </div>
        <!-- 昵称和登录按钮 -->
        <div class="My_user">
          <span>游客</span>
          <van-button @click="goLogin" class="log-btn" size="small" type="primary" color="#21b97a">去登录</van-button>
        </div>
      </div>
    </div>
    <!-- /未登录的头部 -->

    <!-- 已登录的头部 -->
    <div class="My_title" v-else>
      <img :src="'http://liufusong.top:8080' + user.avatar" alt="">
      <div class="My_info My_info2">
        <!-- 头像 -->
        <div class="My_myIcon">
          <img src="http://liufusong.top:8080/img/profile/avatar.png" alt="">
        </div>
        <!-- 昵称和登录按钮 -->
        <div class="My_user">
          <span>{{user.nickname}}</span>
          <van-button @click="quit" size="mini" class="quit-btn" round type="primary" color="#21b97a">退出</van-button>
          <div class="My_edit">编辑个人资料<span><van-icon name="play" /></span></div>
        </div>
      </div>
    </div>
    <!-- /已登录的头部 -->

    <!-- 宫格 -->
    <van-grid :column-num="3" :border="false">
      <van-grid-item :to="isLoading ? '/collect':'/login'" icon="star-o" text="我的收藏" />
      <van-grid-item :to="isLoading ? '/rent':'/login'" icon="wap-home-o" text="我的出租" />
      <van-grid-item icon="clock-o" text="看房记录" />
      <van-grid-item icon="credit-pay" text="成为房主" />
      <van-grid-item icon="manager-o" text="个人资料" />
      <van-grid-item icon="service-o" text="联系我们" />
    </van-grid>

    <!-- 底部图片 -->
    <div class="My_ad">
      <img src="http://liufusong.top:8080/img/profile/join.png" alt="">
    </div>
  </div>
</template>

<script>
import { getUserInfo } from '@/api/user.js'
import { Toast, Dialog } from 'vant'
export default {
  data () {
    return {
      user: {},
      isLoading: false
    }
  },

  created () {
    this.getUserInfo()
  },

  methods: {
    // 退出
    quit () {
      Dialog.confirm({
        title: '好客租房提示你',
        message: '是否确认退出该账号？'
      })
        .then(() => {
          this.$store.commit('removeUser')
          this.isLoading = false
          Toast('账号退出成功！')
        })
        .catch(() => {
          // on cancel
        })
    },
    // 获取用户信息
    async getUserInfo () {
      try {
        const res = await getUserInfo()
        // console.log(res)
        if (res.data.status === 200) {
          this.user = res.data.body
          this.isLoading = true
        } else {
          // Toast('获取用户信息失败！')
          // Toast(res.data.description)
          this.isLoading = false
        }
        // console.log(res)
      } catch (error) {
        Toast('获取用户信息失败！')
      }
    },
    // 去登录
    goLogin () {
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped lang='less'>
.user{
  .My_title{
    min-height: 300px;
    position: relative;
    z-index: 3;
    // 背景图样式
    img{
      width: 100%;
    }
    .My_info{
      position: absolute;
      width: 320px;
      height: 165px;
      background-color: #fff;
      bottom: 0;
      left: 50%;
      transform: translate(-50%);
      box-shadow: 0 0 10px 3px #ddd;

      // 头像样式
      .My_myIcon{
        position: absolute;
        left: 50%;
        transform: translate(-50%,-50%);
        width: 70px;
        height: 70px;
        border-radius: 50%;
        box-shadow: 0 2px 2px #bdbdbd;
        border: 5px solid #f5f5f5;
        background-color: #f5f5f5;
        box-sizing: border-box;
        img{
          width: 60px;
          height: 60px;
          border-radius: 50%;
        }
      }

      // 名称样式
      .My_user{
        height: 100%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: #333;
        span{
          font-size: 13px;
          padding: 13px 0 17px 0;
        }
        .log-btn{
          // margin-top: 20px;
          font-size: 13px;
          height: 30px;
          line-height: 35px;
          padding: 0 15px;
          border-radius: 5px;
        }
      }
    }
    // 已登录样式
    .My_info2{
      height: 208px;
      .My_user{
        span{
        padding: 0 0 5px 0;
        }
        // 按钮
        .quit-btn{
          margin-bottom: 25px;
          width: 54px;
          height: 20px;
          font-size: 12px;
          line-height: 21px;
        }
        .My_edit{
          padding-bottom: 10px;
          font-size: 12px;
          color: #999;
        }
      }
    }
  }
  /deep/.van-grid-item{
    height: 95px;
    .van-grid-item__content{
      color: #333;
      .van-grid-item__icon{
        font-size: 20px;
        margin-bottom: 10px;
      }
      .van-grid-item__text{
        margin: 0;
        font-size: 13px;
      }
    }
  }
  .My_ad{
    margin-top: 10px;
    text-align: center;
    img{
      width: 92%;
    }
  }
}

</style>
